﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace ShortcutShow
{
    /// <summary>
    /// Interaction logic for settingwindows.xaml
    /// </summary>
    public partial class settingwindows : Window
    {
        public bool isSave = true;

        public settingwindows()
        {
            InitializeComponent();

            System.Windows.Forms.NotifyIcon ni = new System.Windows.Forms.NotifyIcon();
            ni.Icon = new System.Drawing.Icon("Main.ico");
            ni.Visible = true;
            ni.DoubleClick +=
                delegate(object sender, EventArgs args)
                {
                    this.Show();
                    this.WindowState = WindowState.Normal;
                };
            AddHandler(Keyboard.KeyDownEvent, (KeyEventHandler)HandleKeyDownEvent);
        }

        private void HandleKeyDownEvent(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Tab && (Keyboard.Modifiers & (ModifierKeys.Control | ModifierKeys.Shift)) == (ModifierKeys.Control | ModifierKeys.Shift))
            {
                MessageBox.Show("CTRL + SHIFT + TAB trapped");
            }

            if (e.Key == Key.Tab && (Keyboard.Modifiers & ModifierKeys.Control) == ModifierKeys.Control)
            {
                MessageBox.Show("CTRL + TAB trapped");
            }
        }

        protected override void OnStateChanged(EventArgs e)
        {
            if (WindowState == WindowState.Minimized)
                this.Hide();

            base.OnStateChanged(e);
        }

        private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            e.Cancel = true;
            WindowState = WindowState.Minimized;
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            System.Diagnostics.Process.GetCurrentProcess().Kill();
        }

        private void loadSettings()
        {
            slider1.Value = Properties.Settings.Default.Opacity;
            System.Drawing.Color a = Properties.Settings.Default.BackColor;
            ColorPicker1.SelectedColor = Color.FromArgb(a.A, a.R, a.G, a.B);
            System.Drawing.Color b = Properties.Settings.Default.FontColor;
            ColorPicker2.SelectedColor = Color.FromArgb(b.A, b.R, b.G, b.B);

            label6.Content = Properties.Settings.Default.Corner;

            textBox1.Text = Properties.Settings.Default.Top.ToString();
            textBox2.Text = Properties.Settings.Default.Left.ToString();

        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            this.WindowState = System.Windows.WindowState.Minimized;
            loadSettings();
            isSave = true;
            button7.Content = "SAVED";
        }

        private void Hyperlink_Click_1(object sender, RoutedEventArgs e)
        {
            try
            {
                System.Diagnostics.Process.Start("mailto:polivka.pavel@gmail.com");
            }
            catch { }
        }

        private void button7_Click(object sender, RoutedEventArgs e)
        {
            Properties.Settings.Default.Corner = label6.Content.ToString();
            Properties.Settings.Default.Opacity = slider1.Value;
            Properties.Settings.Default.Top = Int16.Parse(textBox1.Text);
            Properties.Settings.Default.Left = Int16.Parse(textBox2.Text);
            Properties.Settings.Default.BackColor = System.Drawing.Color.FromArgb(ColorPicker1.SelectedColor.A,ColorPicker1.SelectedColor.R,ColorPicker1.SelectedColor.G,ColorPicker1.SelectedColor.B);
            Properties.Settings.Default.FontColor = System.Drawing.Color.FromArgb(ColorPicker2.SelectedColor.A,ColorPicker2.SelectedColor.R,ColorPicker2.SelectedColor.G,ColorPicker2.SelectedColor.B);
            Properties.Settings.Default.Save();

            isSave = true;
            button7.Content = "SAVED";
        }

        public void notSave(){
            isSave = false;
            button7.Content = "NOT SAVED";
        }

        private void button2_Click(object sender, RoutedEventArgs e)
        {
            label6.Content = "UL";
            notSave();
        }

        private void button3_Click(object sender, RoutedEventArgs e)
        {
            label6.Content = "DL";
            notSave();
        }

        private void button4_Click(object sender, RoutedEventArgs e)
        {
            label6.Content = "UR";
            notSave();
        }

        private void button5_Click(object sender, RoutedEventArgs e)
        {
            label6.Content = "DR";
            notSave();
        }

        private void button6_Click(object sender, RoutedEventArgs e)
        {
            label6.Content = "CUSTOM";
            notSave();
        }

        private void ColorPicker1_SelectedColorChanged(object sender, RoutedPropertyChangedEventArgs<Color> e)
        {
            notSave();
        }

        private void slider1_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
            notSave();
        }

        private void textBox1_TextChanged(object sender, TextChangedEventArgs e)
        {
            notSave();
        }

        private void textBox1_KeyDown(object sender, KeyEventArgs e)
        {

            if (e.Key >= Key.D0 && e.Key <= Key.D9)
            {
                textBox1.Text += e.Key.ToString().Remove(0,1);
            }
            if (e.Key >= Key.NumPad0 && e.Key <= Key.NumPad9)
            {
                textBox1.Text += e.Key.ToString().Remove(0, 6);
            }

        }

        private void textBox2_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key >= Key.D0 && e.Key <= Key.D9)
            {
                textBox2.Text += e.Key.ToString().Remove(0, 1);
            }
            if (e.Key >= Key.NumPad0 && e.Key <= Key.NumPad9)
            {
                textBox2.Text += e.Key.ToString().Remove(0, 6);
            }
        }


    }
}
